package com.bqtt.tumor.demo.business;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bqtt.tumor.demo.common.MyBeanFactory;
import com.bqtt.tumor.demo.data_access.BasicDAO;
import com.bqtt.tumor.demo.data_access.CityDAO;
import com.bqtt.tumor.demo.data_access.DeseaseDAO;
import com.bqtt.tumor.demo.data_access.HealthUserDAO;
import com.bqtt.tumor.demo.entity.City;
import com.bqtt.tumor.demo.entity.MedicalCase;

public class CityService{


	private static CityDAO cityDAO = (CityDAO) MyBeanFactory.getBean("cityDAO");
	private static Logger log = LoggerFactory.getLogger(CityService.class);


	public static long getCodeByName(String name)
	{
		List<City> result = cityDAO.findByProperty("city_name", name);
		
		if(result!=null && result.size()>0)
		{
			return result.get(0).getCity_def_id();
		}
		
		return 0;
	}
	
	public static List<City> getList()
	{
		return cityDAO.findAll();
	}
	
	public static String getNameByCode(long code)
	{
        List<City> result = cityDAO.findByProperty("city_def_id", code);
		
		if(result!=null && result.size()>0)
		{
			return result.get(0).getCity_name();
		}
		
		return "";
	}

	public static List<City> searchByCondition(City condition,int pageSize,int pageNo)
	{
		
		List<City> result = cityDAO.fuzzyFindByProperties(condition.getSQLValidAttributes(),pageSize,pageNo);

		return result;
	}
	
	public static List<City> searchByCondition(City condition,int pageSize,int pageNo,long id)
	{
		
		List<City> result = cityDAO.fuzzyFindByProperties(condition.getSQLValidAttributes(),pageSize,pageNo);
		
		for(City item:result)
		{
			if(item.getCity_def_id()==id)
			{
				item.setDisplayName("inline-block");
			}
			item.fillStringField();
		}
		return result;
	}
	
	public static boolean add(City city)
	{
		try
		{
			cityDAO.save(city);
			return true;
		}
		catch(Exception e)
		{
			log.error(e.getMessage());
	     	return false;	
		}
		
	}
	
	public static boolean update(City city)
	{
		try
		{
			 
			if(city.getCity_def_id()>0)
			{
				cityDAO.update(city);
				return true;
			}
			else
				
			return false;
		}
		catch(Exception e)
		{
			log.error(e.getMessage());
	     	return false;	
		}
		
	}

}
